<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<input type="button" id="btn" value="开/关灯">
<script>
    var btn=document.getElementById("btn");
    var flag=true;
    btn.onclick= function () {
        if(flag){
            document.body.style.backgroundColor="black";
            flag=false;
        }else {
            document.body.style.backgroundColor="white";
            flag=true;
        }
    }
</script>

</body>
</html>